package com.liang.crawlertx2.task;

import com.liang.crawlertx2.pojo.News;
import com.liang.crawlertx2.service.NewService;
import com.liang.crawlertx2.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
@Slf4j
public class NewTask {
    @Autowired
    private HttpClientUtil httpClientUtil;

    @Autowired
    private NewService newService;

    @Scheduled(fixedDelay=1000000)
    public void newTask(){
        log.info("=================爬取开始==================");

        String url = setUrl("西瓜");

        for (int i = 0; i < 5; i++) {
            String html = httpClientUtil.doGetHtml(url + i*10);
            this.parse(html);
        }

        log.info("=================爬取结束==================");
    }

    private static String setUrl(String wd){
        String url="https://www.baidu.com/s?ie=utf-8&medium=0&rtt=1&bsst=1&rsv_dl=news_b_pn&cl=2&wd="+wd+"&tn=news&x_bfe_rqs=03E80&x_bfe_tjscore=0.100000&tngroupname=organic_news&newVideo=12&pn=";
        return url;
    }

    private void parse(String html){
        Document doc = Jsoup.parse(html);
        Elements wrapper = doc.select("div#wrapper_wrapper");
        String total = wrapper.select("div#container>div#header_top_bar>span.nums").text();
        //System.out.println(total);
        Elements elements = wrapper.select("div#container>div#content_left>div>div.result-op");
        for (Element element : elements) {



            String url = element.select("div>h3.news-title_1YtI1>a").attr("href");

            List<News> list = newService.findByUrl(url);
            if(list.size()>0){
                continue;
            }

            log.info("url:{}",url);
            String title = element.select("div>h3.news-title_1YtI1>a").text();
            log.info("title:{}",title);

            String newsSource = element.select("div>div.c-row>div.c-span-last>div.news-source>span.c-color-gray").text();
            String createdTime = element.select("div>div.c-row>div.c-span-last>div.news-source>span.c-color-gray2").text();
            log.info("newsSource:{}",newsSource);
            log.info("createdTime:{}",createdTime);

            String content = element.select("div>div.c-row>div.c-span-last>span").text();
            log.info("content:{}",content);
            log.info("-------------------------------------------");


            News news = new News();
            news.setUrl(url);
            news.setTitle(title);
            news.setNewsSource(newsSource);
            news.setContent(content);
            news.setCreatedTime(createdTime);
            news.setCrawledTime(LocalDateTime.now().plusHours(8));

            newService.saveNew(news);
        }

    }
}
